<template>
	<view class="conter">
		<view class="Abouts">
			<view class="">
				<u--input v-model="newPassword" input-align="right" prefixIcon="密码 : " placeholder="请输入密码" class="input"
					border="surround"></u--input>
			</view>
		</view>
		<!-- 确认密码 -->
		<view class="Abouts">
			<view class="">
				<u--input v-model="confrimNewPassword" input-align="right" prefixIcon="确认密码 : " class="input"
					placeholder="请确认密码" border="surround"></u--input>
			</view>
		</view>
		<button @click="confrim">确认修改</button>
	</view>
</template>

<script>
	import {
		updateByOldPassword,
		updatePassword
	} from '@/api/account/account.js';
	import {
		toast,
		showConfirm,
		tansParams
	} from '@/utils/common';
	import {
		removeToken,
		removeUserAdmin
	} from '@/utils/auth.js';
	export default {
		comments: {},
		data() {
			return {
				systemId: '', //修改密码的系统id
				isOldPassword: '', //密码，根据此字段判断是否之前设置过密码,如果不是'' 则代表有原密码
				oldPassword: '', //用户输入的原密码
				newPassword: '', //新密码
				confrimNewPassword: '' //确认新密码
			};
		},
		onLoad(option) {
			if (option.password) {
				this.isOldPassword = option.password;
				// console.log(this.isOldPassword,"ppp")
			}
			if (option.systemId) {
				this.systemId = option.systemId;
			}
		},
		methods: {
			// 确认
			confrim() {
				if (!this.newPassword) {
					toast('请输入密码');
					return;
				}
				if (this.newPassword != this.confrimNewPassword) {
					toast('两次密码输入不一致');
					return;
				}
				let param = {};
				param.id = this.systemId; //接口需要的参数id
				param.password = this.newPassword; // 接口中的新密码
				updatePassword(param).then((res) => {
					// console.log(res,"pp")
					this.updateSuccess();
				});
				// this.updatePassword();

				// if (this.isOldPassword != '') {
				// 	// 代表需要判断原密码
				// 	this.haveOldPassword();
				// } else {
				// 	// 代表第一次设置密码，只需要判断 密码和确认密码
				// }
			},
			// 使用老密码的更新密码
			haveOldPassword() {
				if (!this.oldPassword) {
					toast('请输入原密码');
					return;
				}
				if (!this.newPassword) {
					toast('请输入密码');
					return;
				}
				if (this.newPassword != this.confrimNewPassword) {
					toast('两次密码输入不一致');
					return;
				}
				let param = {};
				param.id = this.systemId; //接口需要的参数id
				param.newPassword = this.newPassword; // 接口中的新密码
				updateByOldPassword(param).then((res) => {
					this.updateSuccess();
				});
			},
			// 直接更新密码
			updatePassword() {
				if (!this.newPassword) {
					toast('请输入密码');
					return;
				}
				if (this.newPassword != this.confrimNewPassword) {
					toast('两次密码输入不一致');
					return;
				}
				let param = {};
				param.id = this.systemId; //接口需要的参数id
				param.password = this.newPassword; // 接口中的新密码
				updatePassword(param).then((res) => {
					// console.log(res,"pp")
					this.updateSuccess();
				});
			},
			// 更新成功之后
			updateSuccess() {
				removeToken();
				removeUserAdmin();
				toast('更新成功，请重新登录!');
				setTimeout(function() {
					uni.navigateTo({
						url: '/pages/login'
					});
				}, 700);
			}
		}
	};
</script>
<style lang="scss" scoped>
	::v-deep .u-icon__icon {
		font-size: 31rpx !important;
		font-family: PingFang SC-Bold, PingFang SC !important;
		font-weight: 400 !important;
		color: #FFFFFF !important;
	}

	::v-deep .uni-input-input {
		color: #FFFFFF;
	}

	::v-deep .u-input--square {
		border-radius: 0;
	}

	::v-deep .u-input__content {
		margin-bottom: 30rpx;
	}

	.Abouts {
		width: 90%;
		padding: 30rpx 0 0 0;
		display: flex;
		margin: 0 30rpx;
		border-bottom: 0.5rpx solid #282c34;
		justify-content: space-between;
	}

	.conter {
		.input {
			border: none;
			width: 680rpx;
			height: 75rpx;
			opacity: 1;
		}

		button {
			margin-top: 100rpx;
			width: 680rpx;
			height: 77rpx;
			background: linear-gradient(270deg, #4463FF 0%, #3ADDEB 100%);
			box-shadow: 3rpx 3rpx 5rpx 0rpx rgba(0, 0, 0, 0.15);
			border-radius: 14rpx 14rpx 14rpx 14rpx;
			opacity: 1;
			font-size: 28rpx;
			font-family: "font";
			font-weight: 800;
			color: #ffffff;
			line-height: 77rpx;
		}
	}
</style>